home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.cs.arizona.edu
/
ftp.cs.arizona.edu.tar
/
ftp.cs.arizona.edu
/
icon
/
newsgrp
/
group01b.txt
/
000164_icon-group-sender_Thu Nov 8 12:45:09 2001.msg
< prev
next >
Wrap
Internet Message Format
|
2002-01-03
|
3KB
Return-Path: <icon-group-sender>
Received: (from root@localhost)
by baskerville.CS.Arizona.EDU (8.11.1/8.11.1) id fA8JgQI28608
for icon-group-addresses; Thu, 8 Nov 2001 12:42:26 -0700 (MST)
Message-Id: <200111081942.fA8JgQI28608@baskerville.CS.Arizona.EDU>
From: Steve Wampler <swampler@noao.edu>
X-Newsgroups: comp.lang.icon
Subject: mutual evaluation
Date: Thu, 08 Nov 2001 11:08:43 -0700
X-Complaints-To: abuse@noao.edu
X-Accept-Language: en
To: icon-group@cs.arizona.edu
Errors-To: icon-group-errors@cs.arizona.edu
Status: RO
Content-Length: 2300
Since this list has been fairly quiet lately, I thought I post
a couple of (very) simple code snippets showing uses of mutual
evaluation.
I was asked recently by someone how to determine the size of
a file from within Icon. It's really easy (with the newer
releases, at least!) - if f is a file, then
where(seek(f,0))-1
produces the number of bytes in a file. [where() and seek() both
operate on a file using the same positioning used in Icon
when operating on strings... so where(f) returns the position
of the file pointer. To get the number of bytes *upto* that
position you have to subtract 1.]
Ok, but that solution alters the file pointer position within
the file, which may not be desirable. Here's a simple function
that produces the size of a file while preserving the current
file pointer position...
procedure fSize(f)
return 2(oldp := where(f), where(seek(f,0))-1, seek(f,oldp))
end
There's nothing fancy about the use of mutual evaluation here, in
fact, a solution without mutual evaluation isn't much more complex:
procedure fSize2(f)
oldp := where(f)
sizef := where(seek(f,0))-1
seek(f, oldp)
return sizef
end
but you do have to add a second local variable (sizef) to hold the
result of the middle expression in this case.
What if you only have the name of the file? Then:
procedure fileSize(fName)
return 2(f := open(fName), where(seek(f,0))-1, close(f))
end
works. Here, you don't care about the file position but you do want
to close the file when done. Note that if the open(fName) fails,
the procedure call fails as well. Coding the same thing w/o mutual
evaluation can be done as:
procedure fileSize2(fName)
if f := open(fName) then {
sizef := where(seek(f,0))-1
close(f)
return sizef
}
end
The idiom in use in these mutual evaluation examples is simple:
return 2(initialize, compute, cleanup)
which is really pretty clean - most of the time. It's not clean
when any of initialize, compute, or cleanup is complex (but *that*
can often be solved by encapsulating the complex action in a
procedure).
What else could be a problem?
--
Steve Wampler- SOLIS Project, National Solar Observatory
swampler@noao.edu